import { Component } from "react";
import Taro from "@tarojs/taro";
import "./app.scss";
import "taro-ui/dist/style/index.scss";

import { Provider } from "react-redux";
import { combineReducers, legacy_createStore as createStore } from "redux";

const defaultState = {
  satellite: false,
  lon2: 0,
  lat2: 0,
  level: 0
};

const reducers = combineReducers({
  yq: (state = defaultState, action) => {
    if (action.type === "changeScale") {
      return {
        ...state,
        level: action.scale
      };
    }
    if (action.type === "changeSatellite") {
      return {
        ...state,
        satellite: !state.satellite
      };
    }
    if (action.type === "changeMap") {
      console.log(action);
      return {
        ...state,
        lon2: action.lon,
        lat2: action.lat
      };
    }
    return state;
  }
});

const store = createStore(reducers);

class App extends Component {
  componentDidMount() {
    Taro.getUserInfo({
      success: function(res) {
        Taro.setStorage({
          key: "avatarUrl",
          data: res.userInfo.avatarUrl
        });
        Taro.setStorage({
          key: "nickName",
          data: res.userInfo.nickName
        });
      }
    });
    Taro.showShareMenu({
      withShareTicket: true,
      menus: ["shareAppMessage", "shareTimeline"]
    });
  }

  componentDidShow() {}

  componentDidHide() {}

  componentDidCatchError() {}

  // this.props.children 是将要会渲染的页面
  render() {
    return <Provider store={store}>{this.props.children}</Provider>;
  }
}

export default App;
